import nltk
import os


class NLTKManager:
    def __init__(self, data_path=None):
        if data_path:
            os.makedirs(data_path, exist_ok=True)
            nltk.data.path.insert(0, data_path)
            self.data_path = data_path
        else:
            self.data_path = None

    def ensure_resource(self, resource_name):
        try:
            # 尝试不同的路径模式
            paths_to_try = [
                f'corpora/{resource_name}',
                f'tokenizers/{resource_name}',
                f'taggers/{resource_name}',
                f'chunkers/{resource_name}'
            ]
            for path in paths_to_try:
                try:
                    nltk.data.find(path)
                    print(f"✓ '{resource_name}' found at {path}")
                    return True
                except LookupError:
                    continue
            print(f"'{resource_name}' not found, downloading...")
            nltk.download(resource_name, download_dir=self.data_path)
            print(f"'{resource_name}' downloaded successfully")
            return True
        except Exception as e:
            print(f"Error with '{resource_name}': {e}")
            return False

    def get_stopwords(self, language='english'):
        """获取stopwords，确保其存在"""
        if self.ensure_resource('stopwords'):
            from nltk.corpus import stopwords
            return stopwords.words(language)
        else:
            return None


if __name__ == '__main__':
    nltk_manager = NLTKManager('../.venv/nltk_data')
    stop_words = nltk_manager.get_stopwords('english')
    print(f"Loaded {len(stop_words)} stopwords")
